package com.yc.forum.dao;

import com.yc.forum.util.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDao {
    //根据用户的account获取用户信息
    public Map<String,Object>  queryUserMessagesByAccount(String account){
        String sql="select * from user where account=?";
        Map<String,Object> map =DBHelper.selectOneMap(sql,account);
        return map;
    }
    //账号已注册,为空返回假
    public boolean queryRegisterAccount(String account){
        String sql="select * from user where account=?";
        return  !DBHelper.selectListMap(sql,account).isEmpty();
    }
    //账号密码验证，为空返回假
    public boolean querylogin(String name,String pwd){
        String sql="select * from user where account=? and password=?";
        return  !DBHelper.selectListMap(sql,name,pwd).isEmpty();
    }
//    注册
    public void register(String phone,String name,String head_img,String pwd,String realname,
                         String sex,String age,String e_mail){
        List<Object> params = new ArrayList<>();
        String sql="insert into user (account,password,nickname";
        params.add(phone);
        params.add(pwd);
        params.add(name);
        String sql2=") values(?,?,?";
        if(age != null && age.trim().isEmpty()==false){
            sql+=",age";
            sql2+=",?";
            params.add(age);
        }
        if(e_mail != null && e_mail.trim().isEmpty()==false){
            sql+=",email";
            sql2+=",?";
            params.add(e_mail);
        }
        if(head_img != null && head_img.trim().isEmpty()==false){
            sql+=",head_img";
            sql2+=",?";
            params.add(head_img);
        }
        if(sex != null && sex.trim().isEmpty()==false){
            sql+=",sex";
            sql2+=",?";
            params.add(sex);
        }
        if(realname != null && realname.trim().isEmpty()==false){
            sql+=",truename";
            sql2+=",?";
            params.add(realname);
        }
        sql2+=")";
        sql+=sql2;
        DBHelper.update(sql,params.toArray());
    }
    //个人页面分类分页查询
    public Map<String,Object> selectpersonal(String account,String target,String page,String size){
        RedisDao redisDao=new RedisDao();
        UserDao userDao=new UserDao();
        //redis 里查出的id
        List<String> list;
        //查询 return返回结果之一
        List<Map<String,Object>> queryList;
        long total;
        System.out.println(target+"======target的值");
        switch (target)
        {
            //我的点赞
            case "1":
                //查询点赞帖子id
                list=redisDao.queryLikesumPostIdListByAccount(account);
                //查询点赞帖子个数
                total=redisDao.getUserLikesumPostLengthByAccount(account);
                //分页查询点赞帖子
                queryList=userDao.selectSanLianPost(list,page,size);
                break;
            //我的收藏
            case "2":
                list=redisDao.queryPostIdListByAccount(account);
                total=redisDao.getUserCollectionPostLengthByAccount(account);
                queryList=userDao.selectSanLianPost(list,page,size);
                break;
            //我的浏览
            case "3":
                list=redisDao.queryBrowsePostIdListByAccount(account);
                total=redisDao.getUserBrowsePostLengthByAccount(account);
                queryList=userDao.selectSanLianPost(list,page,size);
                break;
            //我的发布
            case "4":
                total=DBHelper.count("select id from post where account=?",account);
                queryList=userDao.selectMyRelease( "post",account, page, size);
                break;
            //我的评论
            case "5":
                total=DBHelper.count("select id from reply where account=?",account);
                queryList=userDao.selectMyRelease("reply",account, page, size);
                break;
            //我的提问
            case "6":
                total=DBHelper.count("select id from problem where account=?",account);
                queryList=userDao.selectMyRelease("problem",account, page, size);
                break;
            default:
                throw new IllegalStateException("target值错误-------Unexpected value: " + target);
        }
        //list 时间字段修改
        Map<String,Object> map=new HashMap<>();
        for (int i = 0; i < queryList.size(); i++) {
            map=queryList.get(i);
            String time=map.get("begintime").toString().substring(0,10);
            map.put("begintime",time);
        }
        System.out.println(total);
        System.out.println(queryList);
        Map<String,Object> pageData=new HashMap<>();
        pageData.put("total",total);
        pageData.put("queryList",queryList);
        return pageData;
    }
    //我发布的帖子,我的评论,我的问题
    private List<Map<String,Object>> selectMyRelease(String table,String account,String page,String size){
        String sql="select * from "+ table+" where account=? order by begintime DESC";
        int start=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        String pagesql=sql+" limit "+start+","+size+"";
        return DBHelper.selectListMap(pagesql,account);
    }
    //查询点赞、收藏、浏览帖子
    private List<Map<String,Object>> selectSanLianPost( List<String> list,String page,String size){
        String[] id=new String[list.size()];
        list.toArray(id);
        //10*2个sql参数
        List<Object> params=new ArrayList<>(20);
        //按in里的值排序
        String sql="select * from post where id in(?,?,?,?,?,?,?,?,?,?) order by field(id,?,?,?,?,?,?,?,?,?,?)";
        int start=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        //取出查询的id
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < Integer.parseInt(size); i++) {
                try{
                    params.add(Integer.parseInt(list.get(i+start)));
                }
                catch (IndexOutOfBoundsException e){
                    params.add(0);
                }
            }
        }
        String pagesql=sql+" limit "+start+","+size+"";
        List<Map<String,Object>> SanLianlist= DBHelper.selectListMap(pagesql,params.toArray());
        return SanLianlist;
    }

    public void modify(String phone, String name, String path, String pwd, String realname, String sex, String age, String e_mail) {
        List<Object> params = new ArrayList<>();
        String sql="update user set password=?,nickname=?";
        params.add(pwd);
        params.add(name);
        String sql2=" where account =?";
        if(age != null && age.trim().isEmpty()==false){
            sql+=",age=?";
            params.add(age);
        }
        if(e_mail != null && e_mail.trim().isEmpty()==false){
            sql+=",email=?";
            params.add(e_mail);
        }
        if(path != null && path.trim().isEmpty()==false){
            sql+=",head_img=?";
            params.add(path);
        }
        if(sex != null && sex.trim().isEmpty()==false){
            sql+=",sex=?";
            params.add(sex);
        }
        if(realname != null && realname.trim().isEmpty()==false){
            sql+=",truename=?";
            params.add(realname);
        }
        sql+=sql2;
        params.add(phone);
        DBHelper.update(sql,params.toArray());
    }
}
